use crate::error::Result;
use crate::model::vo::login_vo::CaptchaVo;
use crate::plugin::captcha::Captcha;
use crate::APPLICATION_CONTEXT;
use std::time::Duration;

pub struct CaptchaService {}

impl CaptchaService {
    pub async fn get_captcha(&self) -> Result<CaptchaVo> {
        let captcha = Captcha::new();
        let cache_code = captcha.code.clone().unwrap_or_default();
        let _ = &APPLICATION_CONTEXT
            .cache_service
            .inner
            .set_str_exp(&cache_code, &cache_code, Some(Duration::from_secs(300)))
            .await;
        Ok(CaptchaVo {
            image: captcha.clone().data.unwrap_or_default().into(),
        })
    }
}
